package top.as.sean.biz.system.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.as.sean.biz.system.IPost;
import annotation.Dict;
import top.as.sean.common.constant.Constant;
import top.as.sean.common.exception.GlobalException;
import top.as.sean.common.id.SnowflakeId;
import top.as.sean.common.response.RespStatus;
import top.as.sean.dao.dto.system.PostAdd;
import top.as.sean.dao.dto.system.PostEdit;
import top.as.sean.dao.dto.system.PostPage;
import top.as.sean.dao.entity.pojo.system.Post;
import top.as.sean.dao.mapper.system.PostMapper;
import top.as.sean.dao.vo.system.PostVo;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * 岗位
 *
 * @author: ArchieSean
 * @create: 2022/11/8
 */
@Slf4j
@Service
public class PostImpl extends ServiceImpl<PostMapper, Post> implements IPost {

    /**
     * 删除职位信息
     *
     * @param ids id
     * @return boolean
     */
    @Override
    public Boolean deleteBatch(List<Long> ids) {
        return removeBatchByIds(ids);
    }

    /**
     * 编辑职位信息
     *
     * @param dto 入参
     * @return boolean
     */
    @Override
    public Boolean editPost(PostEdit dto) {
        Post post = this.lambdaQuery().eq(Post::getPostId, dto.getPostId()).one();
        if (!Optional.ofNullable(post).isPresent()) {
            throw new GlobalException(RespStatus.POST_NOT_FOUND);
        }
        //判断是否更改了编码
        if (!post.getPostCode().equals(dto.getPostCode())) {
            if (validateCode(dto.getPostCode())) {
                throw new GlobalException(RespStatus.POST_EXISTED);
            }
        }
        Post updatePost = new Post();
        BeanUtils.copyProperties(dto, updatePost);
        return updateById(updatePost);
    }

    /**
     * 分页查询职位信息
     *
     * @param dto 入参
     * @return page
     */
    @Override
    @Dict
    public Page<PostVo> getPage(PostPage dto) {
        Page<Post> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        this.lambdaQuery()
                .like(StringUtils.hasLength(dto.getPostCode()), Post::getPostCode, dto.getPostCode())
                .like(StringUtils.hasLength(dto.getPostName()), Post::getPostName, dto.getPostName())
                .eq(ObjUtil.isNotEmpty(dto.getStatus()), Post::getStatus, dto.getStatus())
                .orderByAsc(Post::getPostSort)
                .page(page);
        return (Page<PostVo>) page.convert(item -> {
            PostVo postVo = new PostVo();
            BeanUtils.copyProperties(item, postVo);
            postVo.setStatusDict(item.getStatus());
            return postVo;
        });
    }

    /**
     * 新增职位信息
     *
     * @param dto 入参
     * @return boolean
     */
    @Override
    public Boolean addPost(PostAdd dto) {
        //校验编码是否存在
        if (validateCode(dto.getPostCode())) {
            throw new GlobalException(RespStatus.POST_EXISTED);
        }
        Post post = new Post();
        BeanUtils.copyProperties(dto, post);
        post.setPostId(SnowflakeId.createId());
        return save(post);
    }

    /**
     * 校验职位编码是否存在
     *
     * @param postCode 职位编码
     * @return boolean ,存在：true，不存在：false
     */
    private Boolean validateCode(@NotNull String postCode) {
        return lambdaQuery()
                .eq(Post::getPostCode, postCode)
                .exists();
    }

    /**
     * 查询所有可用岗位信息
     *
     * @return list
     */
    @Override
    public List<Post> getAll() {
        return lambdaQuery()
                .eq(Post::getStatus, Constant.CommonStatus.ENABLE.getCode())
                .list();
    }
}
